package beanstao.util.table;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import beanstao.util.Sber;
import beanstao.util.interfaces.to.IToJson;
import beanstao.util.log.ILog;
import beanstao.util.log.LogHelper;

/**
 * 用一个简单二维表表示数据集合。<br>
 * 该表由Row(行)的集合组成。<br>
 * 列的定义由Meta类型表达,Meta的默认是Long型数据。
 * 
 * @author beanstao@google.com 
 * 
 */
public abstract class Table implements IToJson, ILog
{
	public abstract String getCHSName();

	private final Metas metas;
	private List<Row> rows;

	public Table(Metas metas)
	{
		this.metas = metas;
		if (rows == null)
			rows = new ArrayList<Row>();
	}

	public Metas getMeta()
	{
		return metas;
	}

	public Iterator<Row> getRowIterable()
	{
		return rows.iterator();
	}

	public Row createRow()
	{
		Row row = new Row(metas);
		return row;
	}

	public Table join(Row row)
	{
		rows.add(row);
		return this;
	}

	public void clear()
	{
		rows.clear();
	}

	public void removeAt(int index)
	{
		rows.remove(index);
	}

	public Row getRow(int index)
	{
		return rows.get(index);
	}

	public int getRowCount()
	{
		return rows.size();
	}

	public int getColumnCount()
	{
		return metas.size();
	}

	@Override
	public String toJson()
	{
		Sber sber = Sber.ME();
		for (Row row : rows)
			sber.append(row.toJson()).append(',');
		sber.trimEnd();
		sber.surroundBracket();
		return sber.toString();
	}

	// = 日志接口(ILog)的实现 ============

	@Override
	public void logDebug(Object message)
	{
		LogHelper.logDebug(this, message);
	}

	@Override
	public void logDebug(Object message, Throwable t)
	{
		LogHelper.logDebug(this, message, t);
	}

	@Override
	public void logError(Object message)
	{
		LogHelper.logError(this, message);
	}

	@Override
	public void logError(Object message, Throwable t)
	{
		LogHelper.logError(this, message, t);
	}

	@Override
	public void logInfo(Object message)
	{
		LogHelper.logInfo(this, message);
	}

	@Override
	public void logInfo(Object message, Throwable t)
	{
		LogHelper.logInfo(this, message, t);
	}

	@Override
	public void logWarn(Object message)
	{
		LogHelper.logWarn(this, message);
	}

	@Override
	public void logWarn(Object message, Throwable t)
	{
		LogHelper.logWarn(this, message, t);
	}

}
